package com.smart.util;

import net.sf.json.JSONObject;

import com.smart.constants.SmartConstants;
import com.smart.model.AccessToken;
import com.smart.model.menu.Button;
import com.smart.model.menu.ClickButton;
import com.smart.model.menu.Menu;
import com.smart.model.menu.ViewButton;

public class MenuUtil {
	
	  public static Menu initMenu()
	  {
		  Menu menu=new Menu();
		  ViewButton button11=new ViewButton();
		  button11.setType("view");
		  button11.setName("社区服务");
		  button11.setUrl("http://www.smartcommunitytest.cn/SmartCommunity/views/wechat/home.html");
//		  button11.setUrl("http://www.smartcommunitytest.cn/SmartCommunity/views/wechat/home.html");
		  ViewButton button21=new ViewButton();
		  button21.setType("view");
		  button21.setName("邻里交流");
		  button21.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx67175554c8e02d8e&redirect_uri=http%3A%2F%2Fwww.smartcommunitytest.cn%2Fwechatlogin&response_type=code&scope=snsapi_userinfo&state=123&connect_redirect=1#wechat_redirect");
		  
		  ClickButton button31=new ClickButton();
		  button31.setType("scancode_push");
		  button31.setName("扫码事件");
		  button31.setKey("31");
		  
		  ClickButton button32=new ClickButton();
		  button32.setType("location_select");
		  button32.setName("地理位置");
		  button32.setKey("32");
		  
		  Button button=new Button();
		  button.setName("其他功能");
//		  button.setType("click");
		  button.setSub_button(new Button[]{button31,button32});
		  
		  menu.setButton(new Button[]{button11,button21,button});
		  return menu;
	  }
	  
	  public static int createMenu(String token,String menu)
	  {
		System.out.println("createMenu method");
		 int result=0;
		String url=SmartConstants.CREATE_MENU_URL.replace("ACCESS_TOKEN",token); 
		System.out.println("createMenu method 1");
		JSONObject jsonObject=WeixinUtil.doPostStr(url,menu);
		System.out.println("createMenu method 2");
	    if(jsonObject!=null)
	    {
	    	result=jsonObject.getInt("errcode");
	    	System.out.println("result:"+result);
	    }
	    System.out.println("createMenu finish");
	    return result;
	  }
  
     public static int delMenu(String token)
     {
    	 System.out.println("deleteMenu method");
		 int result=0;
		 String url=SmartConstants.DELETE_MENU_URL.replace("ACCESS_TOKEN",token); 
		 System.out.println("deleteMenu method 1");
		 JSONObject jsonObject=WeixinUtil.doGetStr(url);
		 System.out.println("deleteMenu method 2");
		 if(jsonObject!=null)
		 {
		    	result=jsonObject.getInt("errcode");
		    	System.out.println("result:"+result);
		  }
		  System.out.println("deleteMenu finish");
		  return result;
     }
  
     public static void main(String[] args)
     {
    	 AccessToken accessToken=WeixinUtil.getAccessToken();
    	 String token=accessToken.getToken();
    	 System.out.println("token:"+accessToken.getToken());
   	     System.out.println("expires_in:"+accessToken.getExpires_in());
   	     //int result=delMenu(token);
   	    String menu=JSONObject.fromObject(initMenu()).toString();
   	     int result=createMenu(token,menu);
   	     if(result==0)
	     {
		   System.out.println("create menu success!!");
	     }
	     else
	     {
		  System.out.println("errcode:"+result);  
	     }
     }
}
